const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin");
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');

module.exports = {
  // 入口
  entry: path.resolve(__dirname, '../src/app.js'),

  // 出口
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: 'js/[name].[contenthash].js',
    clean: true,
    publicPath: '/',
  },

  module: {
    rules: [
      {
        test: /\.ejs$/,
        loader: 'ejs-loader',
        options: {
          variable: 'data',
          // interpolate: '\\{\\{(.+?)\\}\\}',
          // evaluate: '\\[\\[(.+?)\\]\\]'
        }
      },
      {
        test: /\.less$/i,
        use: [
          // compiles Less to CSS
          'style-loader',
          'css-loader',
          'less-loader',
        ],
      },
      {
        test: /\.css$/i,
        use: [
          'style-loader',
          'css-loader',
        ],
      },
    ]
  },

  // 插件
  plugins: [
    // 打包html文件
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, '../public/index.html'),
      inject: 'body',
      // filename会导致ejs渲染失败
      // filename: "index.[contenthash].html",
      minify: false,

      data: {
        title: 'zmaolong',
        content: 'Hello World'
      }
    }),

    // 原样拷贝
    new CopyPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, '../public'),
          to: path.resolve(__dirname, '../dist'),
          globOptions: {
            ignore: [
              '**/index.html'

            ],
          },
        },
      ],
    }),




  ],

  resolve: {
    extensions: ['.js', '.json', '.ejs'],
    alias: {
      '@src': path.resolve(__dirname, '../src'),
      '@root': path.resolve(__dirname, '../'),

    },
  }
}